package com.lwc.cfdns.utils.file.upload;

import com.lwc.cfdns.exception.BadException;
import com.lwc.cfdns.utils.SpringContextUtils;
import com.lwc.cfdns.utils.StringUtils;
import com.lwc.cfdns.utils.file.FileTypeUtils;
import com.lwc.cfdns.utils.file.IFileUpload;
import com.lwc.cfdns.utils.file.MimeTypeUtils;
import org.springframework.boot.autoconfigure.web.servlet.MultipartProperties;
import org.springframework.util.unit.DataSize;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.*;

abstract class AbstractFileUpload implements IFileUpload {
    // 单文件上传大小
    private static DataSize maxFileSize = SpringContextUtils.getBean(MultipartProperties.class).getMaxFileSize();
    // 默认允许上传的扩展名
    private static String[] AllowedExtension = MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION;

    public static DataSize getMaxFileSize() {
        return maxFileSize;
    }

    public static String[] getAllowedExtension() {
        return AllowedExtension;
    }

    public static void setAllowedExtension(String[] allowedExtension) {
        AllowedExtension = allowedExtension;
    }

    /**
     * 检测文件是否合法（大小及类型）
     *
     * @param file
     * @param allowedExtension
     * @return Boolean
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/6/4 10:29
     */
    public static Boolean checkFile(MultipartFile file, String[] allowedExtension) {
        if (file.getSize() > getMaxFileSize().toBytes()) {
            throw new BadException("文件大小超限,最大为" + getMaxFileSize());
        }
        String extension = getExtension(file);
        if (extension.equals("")) {
            throw new BadException("获取文件类型失败!");
        }
        if (!isAllowedExtension(extension, allowedExtension)) {
            throw new BadException("上传文件类型不允许!");
        }

        return true;
    }

    /**
     * 生成保存文件路径
     *
     * @return String
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/6/4 10:41
     */
    public static String createPath() {
        String format = new SimpleDateFormat("yyyy/MM/dd").format(new Date());
        return format;
    }

    /**
     * 生成文件名
     *
     * @param file
     * @return String
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/6/4 11:10
     */
    public static String createFileName(MultipartFile file) {
        return UUID.randomUUID().toString() + "." + getExtension(file);
    }

    /**
     * 判断MIME类型是否是允许的MIME类型
     *
     * @param extension
     * @param allowedExtension
     * @return
     */
    public static final boolean isAllowedExtension(String extension, String[] allowedExtension) {
        return Arrays.asList(allowedExtension).contains(extension);
    }

    /**
     * 获取文件名的后缀
     *
     * @param file 表单文件
     * @return 后缀名
     */
    public static final String getExtension(MultipartFile file) {
        String extension = FileTypeUtils.getFileType(file.getOriginalFilename());
        if (StringUtils.isEmpty(extension)) {
            extension = MimeTypeUtils.getExtension(Objects.requireNonNull(file.getContentType()));
        }
        return extension;
    }
}